package edu.pku.sei.transformation.editor.model.base;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.transformation.editor.model.ModelPackage;

public class TypedElement extends NamedElement {
	protected MetaModelElement type;

	@MethodProperty(id = ModelPackage.TYPEDELEMENT_TYPE, catalog = "Variable", name = ModelPackage.TYPEDELEMENT_TYPE, setter = "setType")
	@EnumerationProperty(function = "getTypes")
	public MetaModelElement getType() {
		return type;
	}

	public void setType(MetaModelElement modelType) {
		MetaModelElement oldType = this.type;
		if (oldType != modelType) {
			this.type = modelType;
			firePropertyChange(ModelPackage.TYPEDELEMENT_TYPE, oldType,
					modelType);
			if (this.container != null)
				this.container.firePropertyChange(
						ModelPackage.TYPEDELEMENT_TYPE, oldType, modelType);
		}
	}

	public Object[] getTypes() {
		return new Object[] {};
	}

	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodeNotContain(root,
				ModelPackage.TYPEDELEMENT_TYPE, this.type);
	}

	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.type = (MetaModelElement) XMLUtility.xmlInstance
				.deSerializeNodeNotContain(root,
						ModelPackage.TYPEDELEMENT_TYPE, this);
	}

	@Override
	public void dynamicSet(String featureID, Object value) {
		if (ModelPackage.TYPEDELEMENT_TYPE.equals(featureID)) {
			this.type = (MetaModelElement) value;
		}
		super.dynamicSet(featureID, value);
	}

	@Override
	public String toString() {
		if (type != null)
			return name + ":" + type.toString();
		else if (name != null || !"".equals(name))
			return name;
		else
			return "default";
	}
}
